package com.topfinance.payment.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import com.topfinance.frame.dao.HibernateEntityDao;
import com.topfinance.frame.exception.AppException;
import com.topfinance.frame.util.StringUtil;
import com.topfinance.payment.dto.TC2BisClasMsgDto;
import com.topfinance.payment.ebo.TC2BisClasEbo;
import com.topfinance.payment.ebo.TCfgFormatEbo;
import com.topfinance.payment.service.ITC2BisClasMgr;

/**
 * 该类自动实现了dao的一般方法，如add, remove, update
 * 在service中出现的大部分情况下是在向后台提交数据前的处理(如校验)及获取后台数据后的处理等
 * @author
 *
 */

public class TC2BisClasMgrImpl extends HibernateEntityDao<TC2BisClasEbo> implements ITC2BisClasMgr {

	private static final Log log = LogFactory.getLog(TC2BisClasMgrImpl.class);
	
	//获得传输协议	
	public TC2BisClasEbo getTC2BisClas(Integer oid) throws AppException{
		return (TC2BisClasEbo)get(oid);
	}

	public void deleteTC2BisClas(Integer oid) throws AppException{
		removeById(TC2BisClasEbo.class,oid);
	}

	//获得业务种类对象
	//参数：tpcode=业务类型，clascode=业务种类，apptype=应用
	public TC2BisClasEbo getBisClasEbo(String tpcode,String clascode,String apptype) throws AppException{
		if(StringUtil.isNullStr(tpcode)||StringUtil.isNullStr(clascode)||StringUtil.isNullStr(apptype))
			throw new AppException("[业务类型，种类和应用模块都不能为空]");
		String hql = " from TC2BisClasEbo t " ;
		String where = " where t.TC2BisType.tpCode = '" + tpcode + "'";
		where += " and t.clsCode = '" + clascode + "'";
		where += " and t.appType = '" + apptype + "'";
		List<Object> paramList = new ArrayList<Object>();
		List<TC2BisClasEbo> clas =  find(hql + where,paramList.toArray());
		if(clas.size() == 1)
			return clas.get(0);
		else if(clas.size() == 0)
			return null;
		else
			throw new AppException("[组合没有找到业务种类对象]tpcode="+tpcode+",clascode="+ clascode + ",apptype="+ apptype );
	}

	//获得业务种类列表
	public List<TC2BisClasEbo> getAllBisClas() throws AppException{
		String hql = " from TC2BisClasEbo t " ;
		String where = "";
		List<Object> paramList = new ArrayList<Object>();
		if (isNotBlank(where)) {
			where = " where " + where;
		}
		hql = hql + where;
		return find(hql,paramList.toArray());		
	}
	
	//获得业务种类
	public List<TC2BisClasEbo> getAllBisClas(String catCode,String tpCode,String appType) throws AppException{
//		Criteria crtia = this.getSession().createCriteria(TC2BisClasEbo.class);
//		if (catCode != null) 
//			crtia.add(Restrictions.eq("TC2BisCatg.catCode", catCode));
//		if (tpCode != null) {
//			crtia.add(Restrictions.eq("TC2BisType.tpCode", tpCode));
//			crtia.add(Restrictions.eq("TC2BisCatg.catCode", tpCode.subSequence(0, 1)));
//		}
//		if (appType != null) 
//			crtia.add(Restrictions.eq("appType", appType));
//		return crtia.list();
			
		String hql = " from TC2BisClasEbo bc " ;
		String where = "";
		if (catCode != null)
			where += " and bc.TC2BisCatg.catCode ='" + catCode + "'";
		if (tpCode != null) 
			where += " and bc.TC2BisType.tpCode = '" + tpCode + "'";
		if (appType != null) 
			where += " and bc.appType = '" + appType + "'";
		List<Object> paramList = new ArrayList<Object>();
		if (isNotBlank(where)) {
			where = " where " + where.substring(4);
		}
		hql = hql + where;
		return find(hql,paramList.toArray());		
	}
	
	//获得业务种类-报文对应
	public List<TC2BisClasMsgDto> getAllBisClasMsg(String catCode,String tpCode,String appType) throws AppException{
		String hql = " select new com.topfinance.payment.dto.TC2BisClasMsgDto(c.TC2BisType.tpCode,c.clsCode,c.name,m.msgCode,m.msgName,c.appType) ";
		hql += " from TC2BisClasEbo c,TC2BisTpMsgEbo m " ;
		String where = " where c.TC2BisType.tpCode = m.tpCode and c.appType = m.appType and m.isAcitve = 'Y' ";
		if (StringUtil.isNotNullStr(tpCode))
			where += " and c.TC2BisType.tpCode ='" + tpCode + "'";
		if (StringUtil.isNotNullStr(appType)) 
			where += " and c.appType = '" + appType + "'";
		List<Object> paramList = new ArrayList<Object>();
		hql = hql + where;
		return find(hql,paramList.toArray());		
	}

}
